package com.jiyinit.web.core.business.dao;

import com.jiyinit.core.framework.dao.mybatis.base.MybatisDaoContext;
import com.jiyinit.web.core.business.dto.FriendGroupRequest;
import com.jiyinit.web.core.business.dto.FriendRequest;
import com.jiyinit.web.core.business.dto.ReadMessageRequest;
import com.jiyinit.web.core.business.entity.TabCoreUserEntity;
import com.jiyinit.web.core.business.entity.TabSendMessageEntity;
import com.jiyinit.web.core.business.entity.TabUserFriendEntity;
import org.mybatis.spring.SqlSessionTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;

/**
 * <p></p>
 * <p/>
 * <PRE>
 * <BR>	修改记录
 * <BR>-----------------------------------------------
 * <BR>	修改日期			修改人			修改内容
 * </PRE>
 *
 * @author zhaolaiwei
 * @version 1.0
 * @date 2017/4/11 17:51
 * @since 1.0
 */
@Repository("core.userFriend")
public class UserFriendDao {

    /**
     * 日志对象
     */
    private static Logger logger = LoggerFactory.getLogger(UserFriendDao.class);

    private String SQLID = "core.userfriend.";

    @Autowired
    @Qualifier("core.MybatisDaoContext")
    private MybatisDaoContext mybatisDaoContext;

    public List<TabUserFriendEntity> findFriends(Integer userId){
        return mybatisDaoContext.findAll(SQLID+"findFriends",TabUserFriendEntity.class,userId);
    }

    public List<TabUserFriendEntity> findAppFriends(Integer userId){
        return mybatisDaoContext.findAll(SQLID+"findAppFriends",TabUserFriendEntity.class,userId);
    }

    public int addFriend(TabUserFriendEntity friend){
        return mybatisDaoContext.save(SQLID+"insertSelective",friend);
    }

    public List<TabUserFriendEntity> hasFriends(Integer userId,Integer friendId){
        HashMap<String,Object> params = new HashMap();
        params.put("userId",userId);
        params.put("friendId",friendId);
        return mybatisDaoContext.findAll(SQLID+"findFriends",TabUserFriendEntity.class,params);
    }

    public List<TabUserFriendEntity> isFriend(Integer userId,Integer friendId){
        HashMap<String,Object> params = new HashMap();
        params.put("userId",userId);
        params.put("friendId",friendId);
        return mybatisDaoContext.findAll(SQLID + "isFriend", TabUserFriendEntity.class, params);
    }

    public int agreeFriend(FriendRequest friendRequest){
        HashMap<String,Object> params = new HashMap();
        params.put("userId",friendRequest.getUserId());
        params.put("friendId",friendRequest.getFriendId());
        return mybatisDaoContext.update(SQLID+"agreeFriend",params);
    }

    public int update(FriendGroupRequest groupRequest){
        HashMap<String,Object> params = new HashMap();
        params.put("userId",groupRequest.getUserId());
        params.put("friendId",groupRequest.getFriendId());
        params.put("friendGroupId",groupRequest.getFriendGroupId());
        return mybatisDaoContext.update(SQLID+"addGroupId",params);
    }

    public int getFriendMsg(Integer userId){
        return mybatisDaoContext.findOne(SQLID+"getFriendMsg",Integer.class,userId);
    }
}
